/*
 * table_CppMacroCtrl_t.c
 *
 *  Created on: 2012/04/01
 *      Author: guanxiaoying
 */
#include "../../parser/coredef.h"
/*  KIND_MACROCTRL */
typedef struct _MACROCTRL
{
	AST			astMacroDef;		/* 2 */
	RBTREE			rbtree;			/* 3 */
} MACROCTRL, *PMACROCTRL;
#define TableSize  (sizeof(MACROCTRL))
#define TableType  PMACROCTRL
#define stTable stCppMacroCtrlManager.memoryBlocks
#define maxTable stCppMacroCtrlManager.maxRecordNum
#define  maxNumPerBlk  (stCppMacroCtrlManager.astMaxNum_Block)

/* private field */
static AST  gblMacroCtrl=0;
static TABLEMANAGER stCppMacroCtrlManager = {
//		U4                  usIdx;
		0,
//		PSZ				    con_pszName;    constant
		__FILE__,
//		U2					con_usSize;		constant
		TableSize,
//		U2					block_num;	　　　　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_Block;     １回設定のみ
		0,
//		S4					astTabNum_LastPage; 　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_LastPage;     Reset clear  際　要
		0,
//		U4					ulMaxByte;	　　　　　　　　　　クリアしない
		0,
//		U4					con_minPageSize;　　　　　　　　　　constant
		0
};

static VOID resetListener( U4  procid, U2 index )
{
	if( procid == stCppMacroCtrlManager.u4Idx )
	{
		if( index > 0 && index < MAX_VOLUMN_OF_TABLE )
		{
			stTable[ index-1] = NULL;
		}
	}
}
static TableType core_getTable( AST index )
{
	TableType temp = (TableType)Check_Com( &stCppMacroCtrlManager,  resetListener, index );
	if( temp != NULL ){ return &temp[index%maxNumPerBlk]; } return NULL;
}
static BOOL ClearTable( VOID )
{
	gblMacroCtrl = 0;
	return Clear_Com( &stCppMacroCtrlManager );
}
static BOOL ResetTable( VOID )
{
	gblMacroCtrl = 0;
	return Reset_Com( &stCppMacroCtrlManager );
}
static BOOL AllocTable( AST astReqArg )
{
	return AllocTable_Com( &stCppMacroCtrlManager, astReqArg, resetListener );
}
static AST getMaxLineNum( VOID )
{
	return maxTable;
}
static AST getLeft( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astLeftChild;
}
static AST setLeft( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astLeftChild = value;
}
static AST getRight( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astRightChild;
}
static AST setRight( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astRightChild = value;
}
static AST getParent( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astParent;
}
static AST setParent( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astParent = value;
}
static U2 getColor( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.lColor;
}
static U2 setColor( AST index, U2 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.lColor = value;
}

/* give up the way that defining get/set function pointer in the struct,
 * because of memory volume.
 */
//ACCESS_IDX_FIELD macroInfo;
static AST  getMacroInfo( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astMacroDef;
}
static AST  setMacroInfo( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astMacroDef = value;
}
static ACCESS_RBTREE stAccessRbtree =
{
		AllocTable,
		getMaxLineNum,
		getMacroInfo,
		{
				getLeft,
				setLeft
		},
		{
				getRight,
				setRight
		},
		{
				getParent,
				setParent
		},
		{
				getColor,
				setColor
		}
};
static S4  cmpMacroName( AST astMacroDefInfo, void * pstrName )
{
	/* KIND_MACROCTRL のIDX_INDEX  =>  KIND_MACRODEFINFO   */
	PSZ  pszDefinedName;
	PSZ  pszNewName;
	S4 ret;

	pszNewName = ( PSZ )pstrName;
	if( pszNewName == NULL )
	{
		return RBT_LESS;
	}
	pszDefinedName = stCoreMacroDefInfoAccess.macroName.get( astMacroDefInfo );
	ret = strcmp(pszDefinedName, pszNewName );
	if( ret == 0 )
	{
		return RBT_FOUND;
	}
	if( ret < 0 )
	{
		return RBT_LESS;
	}
	return RBT_LARGER;
}
static BOOL   findMacro( PSZ name, AST * pastMacroCtrl)
{
	AST astResult;
	S4 ret;
	ret = FindData( &stAccessRbtree, gblMacroCtrl, name, &astResult, cmpMacroName );
	if( pastMacroCtrl != NULL )
	{
		*pastMacroCtrl = astResult;
	}
	if( ret == RBT_FOUND )
	{
		return TRUE;
	}
	return FALSE;
}
static S4   addMacro( AST astMacroDef )
{
	AST astResult;
	S4 find_result;
	AST nMaxIndex=0;
	PSZ name = stCoreMacroDefInfoAccess.macroName.get( astMacroDef );
	BOOL bret;

	find_result = FindData( &stAccessRbtree, gblMacroCtrl, name, &astResult, cmpMacroName );
	if( find_result == RBT_FOUND )
	{
		setMacroInfo( astResult, astMacroDef);
		return COEXIT_SUCCESS;
	}
	bret =  InsertData( &stAccessRbtree, find_result, astResult, &gblMacroCtrl, &nMaxIndex );
	if( bret == FALSE  )
	{
		return (COEXIT_FATAL);
	}
	setMacroInfo( nMaxIndex, astMacroDef );
	return COEXIT_SUCCESS;
}
const CPPMACROCTRL_ACCESS stCppMacroCtrlAccess = {
		ClearTable,
		ResetTable,
		AllocTable,
		getMaxLineNum,
		findMacro,
		addMacro,
		{
				getMacroInfo,
				setMacroInfo
		},
		&stAccessRbtree
};

